<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class EntryController extends Controller
{
    use AuthenticatesUsers;
    protected $maxAttempts = 3;
    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function guard()
    {
        return Auth::guard('member');
    }

    public function showLoginForm()
    {
        return view('home.auth.login');
    }

    public function loggedOut(Request $request)
    {
        return redirect('/');
    }

    protected function validateLogin(Request $request)
    {
        $messages = [
            "{$this->username()}.required" => '邮箱不能为空',
            'password.required' => '密码不能为空',
            'captcha.required' => '验证码不能为空',
            'captcha.captcha' => '验证码错误',
        ];

        $this->validate($request, [
            $this->username() => 'required|string',
            'password' => 'required|string',
            'captcha' => ['required', 'captcha'],
        ], $messages);
    }

    public function username()
    {
        return 'email';
    }
}
